﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ylh.BLL;
using System.Reflection;

using System.Data;

public partial class SignIn : PageBase
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SystemInfoBLL SysInfo = new SystemInfoBLL(DbConnectionStr);
            //获取指定的数据库中的VersionCode
            lbVersion.Text = SysInfo.GetVersionCode();
        }
    }

    protected void LoginButton_Click(object sender, ImageClickEventArgs e)
    {
        StaffBLL staffbll = new StaffBLL(DbConnectionStr);

        if (!IsNeedUpdateDatabase())
        {

            if (string.IsNullOrEmpty(UserName.Text.Trim()))
            {
                FailureText.Text = "账号不能为空";
                return;
            }
            if (string.IsNullOrEmpty(Password.Text.Trim()))
            {
                FailureText.Text = "系统不允许空密码";
                return;
            }

            DataTable dt = staffbll.IsLogin(UserName.Text.Trim(), Password.Text.Trim());

            if (dt.Rows.Count == 0)
            {
                FailureText.Text = "账号或者密码不正确!";
                 Password.Text = string.Empty;
                return;
            }

            //此处验证码需要取消注释
            if (txtValidateCode.Text.ToUpper() != Request.Cookies["CheckCode"].Value.ToString())
            {
                FailureText.Text = "验证码错误！";
                txtValidateCode.Text = string.Empty;
                return;
            }

            SetLoginInfo(dt);

            //this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "", "windowopen()", true);
            RedirectIndexPage();
            //Response.Redirect("Temp.aspx");
        }
        else
        {
            Response.Redirect("UpdateDataBase.aspx");
        }
    }

    protected void RedirectIndexPage()
    {
        string s = string.Empty;

        s = s + "<script type=\"text/javascript\">" + "\r\n";


        s = s + "window.open(\"main.htm\",\"\",\"width=\" + (screen.availWidth - 8) + \",height=\" + (screen.availHeight-34) + \",top=0,left=0,'fullscreen=yes,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,status=no\");closeme();" + "\r\n";
        s = s + "</script>";
        ScriptManager.RegisterStartupScript(this, this.GetType(), this.Page.ToString(), s, false);
    }

    #region MyRegion
    /// <summary>
    /// 判断当前是否需要升级
    /// </summary>
    /// <returns></returns>
    private bool IsNeedUpdateDatabase()
    {
        SystemInfoBLL SysInfo = new SystemInfoBLL(DbConnectionStr);

        //获取指定的数据库中的VersionCode
        string VersionCode = SysInfo.GetVersionCode();

        bool IsNeedUp = false;

        Assembly assemble = Assembly.LoadFrom(Server.MapPath("Bin/UpdateDatabase.dll"));
        Type[] theassem = null;
        if (VersionCode == "")
        {
            theassem = assemble.GetTypes().OrderBy(th => th.Name).ToArray<Type>();
        }
        else
        {
            theassem = assemble.GetTypes().Where(th => Convert.ToDecimal(th.Name.Replace("UpdateDatabase", "")) >= Convert.ToDecimal(VersionCode.Substring(0, 6))).ToArray();
        }

        decimal MaxVersion = Convert.ToDecimal(VersionCode);


        foreach (Type t in theassem)
        {
            object obj = Activator.CreateInstance(t, new object[] { }); //创建一个带参数的实例
            MethodInfo[] me = t.GetMethods().OrderBy(th => th.Name).Where(th => th.Name.Contains("ExecuteSQL") && GetVersion(th.Name) > Convert.ToDecimal(VersionCode)).ToArray();
            if (me.Length > 0) { IsNeedUp = true; break; }
        }
        return IsNeedUp;
    }
    /// <summary>
    /// 获取版本信息
    /// </summary>
    /// <param name="name">当前方法的名称</param>
    /// <returns></returns>
    public decimal GetVersion(string name)
    {
        return Convert.ToDecimal(name.Replace("ExecuteSQL", ""));
    }
    #endregion


    private void SetLoginInfo(DataTable dt)
    {
        Session["StaffID"] = dt.Rows[0]["StaffID"].ToString();
        Session["DepartmentID"] = dt.Rows[0]["DepartmentID"].ToString();
        Session["RoleID"] = dt.Rows[0]["RoleID"];
        Session["StaffName"] = dt.Rows[0]["StaffName"];
        Session["StaffLoginName"] = dt.Rows[0]["StaffLoginName"].ToString();
        //获取相关权限,后面解决

    }

}